package org.example.service.tree.impl;

import org.example.service.tree.NameProvider;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;
import org.jsoup.nodes.TextNode;

public class HtmlNameProvider implements NameProvider<Node> {
    @Override
    public String getName(Node node) {
        if (node instanceof Element) {
            Element element = (Element) node;
            StringBuilder nameBuilder = new StringBuilder(element.tagName());
            if (element.hasAttr("id")) {
                nameBuilder.append("#").append(element.id());
            }
            return nameBuilder.toString();
        } else if (node instanceof TextNode) {
            TextNode textNode = (TextNode) node;
            String text = textNode.text().trim();
            return text;  // 用引号包裹文本内容
        } else {
            return node.toString();
        }
    }
}
